import store from '../store';
import dayjs from 'dayjs';
//一些公共方法
class mStorage {
  static SetValue(key, val, type = 1) {
    let _Storage = mStorage.GetStorageByType(type)
    if (!_Storage) {
      return _Storage
    }
    _Storage.setItem(key, JSON.stringify(val));
  }
  static GetValue(key, type = 1) {
    let _Storage = mStorage.GetStorageByType(type)
    if (!_Storage) {
      return _Storage
    }
    let valjson = _Storage.getItem(key);
    if (valjson){
      return JSON.parse(valjson);
    }
    else{
      return undefined;
    }
  }
  static Remove(key, type = 1) {
    let _Storage = mStorage.GetStorageByType(type)
    if (!_Storage) {
      return _Storage
    }
    _Storage.removeItem(key);
  }
  static Clear(type = 1) {
    let _Storage = mStorage.GetStorageByType(type)
    if (!_Storage) {
      return _Storage
    }
    _Storage.clear();
  }
  static GetStorageByType(type = 1) {
    if (type == 1) {
      return window.localStorage
    }
    else if (type == 2) {
      return window.sessionStorage
    }
    else {
      return undefined
    }
  }
}
class common_apis{
  //任务运行状态status 1 没有运行 2运行中  3运行成功 4运行失败
  static Get_status_color_by_status(status){
    if(status==1 || status==2){
      return "#262626"
    }
    else if(status==3){
      return "#52c41a"
    }
    else if(status==4){
      return "#ff4d4f"
    }
    else{
      return "#262626"
    }
  }
  static Get_ackName_By_ack_type(status){
    let find_index=store.state.ack_type.findIndex((val)=>{
      return  val.value==status
    })
    if(find_index>=0){
      return store.state.ack_type[find_index].name
    }
    else{
      return ""
    }
  }
  static Get_xc_Time(_ticks){
    if(_ticks>0){
      let ticks = _ticks / 1000; //秒数
      let days = ticks / (24 * 60 * 60);
      let d_show = parseInt(days);
      ticks = ticks - d_show * 24 * 60 * 60;
      let hours = ticks / (60 * 60);
      let h_show = parseInt(hours);
      ticks = ticks - h_show * 60 * 60;
      let ms = ticks / 60;
      let m_show = parseInt(ms);
      ticks = parseInt(ticks - m_show * 60);
      let Date_format = `${d_show}天 ${h_show < 10 ? `0${h_show}` : h_show}:${m_show < 10 ? `0${m_show}` : m_show}:${ticks < 10 ? `0${ticks}` : ticks}`;
      return Date_format
    }
    else{
      return ""
    }
  }
  static Get_pre_dir(_dir){
    if(_dir){
      let avtive_dir=_dir.replaceAll('//', '/');
      let lastpos = avtive_dir.lastIndexOf('/');
      if(lastpos>=2){
        return {
          pre:avtive_dir.substr(0, lastpos),
          name:avtive_dir.substr(lastpos),
        }
      }
    }
    return undefined
  }
  static parse_ini_to_value(value, key_set, except_key1, except_key2){
    let array=[]
    for (const [key2, value2] of Object.entries(value)) {
      let is_add=true;
      if(key_set== except_key1){
        let index= except_key2.findIndex((val) => { 
          return val == key2 
        })
        if(index >= 0){
          is_add=false
        }
      }
      if(is_add){
        let p_value ={}
        if(Array.isArray(value2)){
          p_value = {
            type: "input",
            name: key2,
            default: "" +value2[0],
            description: value2[1]
          }
          if (value2.length == 3) {
            p_value.type = "select"
            let splits = value2[2].split("&")
            p_value.select = []
            for (let i = 0; i < splits.length; i++) {
              p_value.select.push({
                name: splits[i],
                value: "" +splits[i]
              })
            }
          }
        }
        else{
          p_value = {
            type: "input",
            name: key2,
            default: ""+value2,
            description: key2
          }
        }
        array.push(p_value)
      }
    }
    return array;
  }
  static parse_value_to_ini(parmars_array){
    if(parmars_array?.length){
      let object1 = {}
      for (let j = 0; j < parmars_array?.length; j++) {
        let one_parmar = parmars_array[j];
        let unparse_values = [one_parmar.default, one_parmar.description]
        if (one_parmar.type == "select") {
          let select_values = []
          for (let i = 0; i < one_parmar.select.length; i++) {
              select_values.push(one_parmar.select[i].name)
          }
          unparse_values.push(select_values.join("&"))
        }
        object1[one_parmar.name] = unparse_values
      }
      return object1
    }
    return undefined
  }
  static randomRgbColor(){
    let r = common_apis.randomInteger(255);
    let g = common_apis.randomInteger(255);
    let b = common_apis.randomInteger(255);
    return [r, g, b];
  }
  static randomInteger(max) {
    return Math.floor(Math.random() * (max + 1));
  }
  static GetTreeGeometry(dataRef) {
    let geometry_array=[]
    if (dataRef?.children && Array.isArray(dataRef?.children) && dataRef?.children?.length){
      for (let i = 0; i < dataRef?.children?.length; i++) {
        let one_children = dataRef?.children[i];
        let one_children_geometry_array= common_apis.GetTreeGeometry(one_children)
        geometry_array = [].concat(geometry_array, one_children_geometry_array)
      }
    }
    else{
      if (dataRef?.data?.geometry){
        geometry_array.push(dataRef?.data?.geometry)
      }
    }
    return geometry_array
  }
  static GetTreeTitle(dataRef) {
    let geometry_array = []
    if (dataRef?.children && Array.isArray(dataRef?.children) && dataRef?.children?.length) {
      for (let i = 0; i < dataRef?.children?.length; i++) {
        let one_children = dataRef?.children[i];
        let one_children_geometry_array = common_apis.GetTreeTitle(one_children)
        geometry_array = [].concat(geometry_array, one_children_geometry_array)
      }
    }
    else {
      if (dataRef?.title) {
        geometry_array.push(dataRef?.title)
      }
    }
    return geometry_array
  }
  static stirng_array_to_array(_input) {
    let _inputs = []
    if (_input) {
      if (Array.isArray(_input)) {
        _inputs = _input
      }
      else if (typeof _input === "string" || typeof _input === "number" || typeof _input === "object") {
        _inputs.push(_input)
      }
    }
    return _inputs
  }
  static read_xml_to_workbook(_file) {
    return new Promise((resolve) => {
      var fileReader = new FileReader();
      fileReader.onload = function (ev) {
        try {
          var data = ev.target.result
          var workbook = XLSX.read(data, {
            type: 'binary'
          }) // 以二进制流方式读取得到整份excel表格对象
          resolve(workbook)
        } catch (e) {
          resolve(false)
          console.log('文件类型不正确');
        }
      }
      fileReader.readAsBinaryString(_file);
    })
  }
  
  
  
  
}
export function utils_set(){
  global["vapp"].config.globalProperties.$common_apis =common_apis
}
